package cn.tedu.system.product.service.impl;

import cn.tedu.system.base.exception.ServiceException;
import cn.tedu.system.base.response.StatusCode;
import cn.tedu.system.product.mapper.ProductMapper;
import cn.tedu.system.product.pojo.dto.AgricultureProductParam;
import cn.tedu.system.product.pojo.dto.LiveProductParam;
import cn.tedu.system.product.pojo.entity.LiveProduct;
import cn.tedu.system.product.pojo.entity.Product;
import cn.tedu.system.product.pojo.vo.LiveProductVO;
import cn.tedu.system.product.pojo.vo.ProductVO;
import cn.tedu.system.product.service.ProductService;
import cn.tedu.system.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<ProductVO> selectAll(HttpSession session) {
        log.debug("用户查看上架商品业务");
        UserVO userVO = (UserVO) session.getAttribute("user");
        List<ProductVO> productVOList = productMapper.selectPut();
        return productVOList;
    }

    @Override
    public ProductVO selDetail(Long id, HttpSession session) {
        log.debug("查看商品详情业务:{}",id);
        UserVO userVO = (UserVO) session.getAttribute("user");
        ProductVO productVO = productMapper.selectDetail(id);
        return productVO;
    }

    //杨平
    //搜索商品
    @Override
    public List<ProductVO> search(String name, HttpSession session) {
        log.debug("搜索商品业务:{}",name);
        UserVO userVO = (UserVO) session.getAttribute("user");

        List<ProductVO> productVOList = productMapper.searchProduct(name);
        if(productVOList.size()>0){
            return productVOList;
        }
        throw new RuntimeException("没有该商品");
    }

    //直播商品发布
    @Override
    public void addProduct(LiveProductParam liveProduct,  HttpSession session) {
        log.debug("直播商品发布业务: {}",liveProduct);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }

        LiveProduct  live = new LiveProduct();
        BeanUtils.copyProperties(liveProduct,live);
        live.setStatus(1);
        live.setCreateTime(new Date());

        if (productMapper.addLiveProduct(live)<=0){
            throw new ServiceException(StatusCode.FAILED,"直播商品添加失败");
        }
    }

    //查看直播商品
    @Override
    public List<LiveProductVO> selectLiveProduct(Long liveId, HttpSession session) {
        log.debug("查看直播商品业务: {}",liveId);
        UserVO userVO = (UserVO) session.getAttribute("user");

        List<LiveProductVO> liveProductVOList = productMapper.selectLiveProduct(liveId);
        return liveProductVOList;
    }

    //下架直播商品
    @Override
    public void delLiveProduct(Long id, HttpSession session) {
        log.debug("下架直播商品业务: {}",id);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }
        if (productMapper.delLiveProduct(id)<=0){
            throw new ServiceException(StatusCode.FAILED,"下架失败");
        }
    }

    //管理员发布助农商品
    @Override
    public void addAgricultureProduct(AgricultureProductParam productParam, HttpSession session) {
        log.debug("管理员发布助农商品业务: {}",productParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }
        if (userVO.getRole()!=3){
            throw new ServiceException(StatusCode.FAILED,"权限不足");
        }

        Product product = new Product();
        BeanUtils.copyProperties(productParam,product);
        product.setStatus(1);
        product.setSales(0);
        product.setIsAgriculture("1");
        product.setCreateTime(new Date());
        product.setUpdateTime(new Date());
        if (productMapper.addAgricultureProduct(product)<=0){
            throw new ServiceException(StatusCode.FAILED,"助农商品添加失败");
        }
    }

    @Override
    public void delAgricultureProduct(Long id, HttpSession session) {
        log.debug("管理员删除助农商品业务: {}",id);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }
        if (userVO.getRole()!=3){
            throw new ServiceException(StatusCode.FAILED,"权限不足");
        }
        if (productMapper.delAgricultureProduct(id)<=0){
            throw new ServiceException(StatusCode.FAILED,"助农商品删除失败");
        }
    }
}
